<template>
  <el-dialog v-loading="loading" class="assign-role" title="分配角色" :visible="showRoleDialog" @open="handleOpen" @close="handleClose">
    <!-- <el-checkbox-group v-mode="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group> -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align:right">
        <el-button @click=" handleClose">取 消</el-button>
        <el-button type="primary" @click="clickSubmit = false">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 导入获取角色列表,获取用户的基本信息,给员工分配角色
import { reqGetRoleList } from '@/api/setting'
import { reqGetUserDetailById } from '@/api/user'
import { reqAnewAssignRole } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleList: [], // 所有角色
      checkList: [], // 选中角色
      loading: false
    }
  },
  created() {
    this.getRoleList()
    this.getUserDetailById()
  },
  methods: {
    //   关闭弹窗
    handleClose() {
      // this.showRoleDialog = false
      this.$emit('update:showRoleDialog', false)
    },
    handleOpen() {
      this.loading = true
      //  当两个异步全部执行完毕时,才调用then中的回调函数
      Promise.all([this.getRoleList(),
        this.getUserDetailById()]).then(() => {
        this.loading = false
      })
    },
    // 获取所有的角色列表
    async getRoleList() {
      const { data } = await reqGetRoleList(1, 100)
      this.roleList = data.rows
      // console.log(this.roleList)
    },
    // 获取员工已存在角色
    async getUserDetailById() {
      const { data: { roleIds }} = await reqGetUserDetailById(this.userId)
      this.checkList = roleIds || []
    },
    // 获取重新
    async clickSubmit() {
      await reqAnewAssignRole({ id: this.userId, roleIds: this.checkList })
      this.handleClose()
    }
  }
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
